import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '../views/layout/Index.vue'
import Home from '../views/home/Home.vue'
import Admin from '../views/admin/Admin.vue'
import User from '../views/user/User.vue'
import Membershiptype from '../views/user/membershiptype/Membershiptype.vue'
import Userlist from '../views/user/userlist/Userlist.vue'
import Booking from '../views/booking/Booking.vue'
import Appointmentslot from '../views/booking/appointmentslot/Appointmentslot.vue'
import Service from '../views/booking/service/Service.vue'
import Login from '../views/login/Login.vue'
import Update from '../views/update/Update.vue'
import Oder from '../views/oder/Oder.vue'
import Merchandise from '../views/oder/merchandise/Merchandise.vue'
import Refund from '../views/oder/refund/Refund.vue'
import Serviceappointment from '../views/oder/serviceappointment/Serviceappointment.vue'
import Petknowledge from '../views/petknowledge/Petknowledge.vue'
import Homecarousel from '../views/homecarousel/Homecarousel.vue'
import Pettype from '../views/pettype/Pettype.vue'
import Product from '../views/product/Product.vue'
import Addorder from '../views/oder/merchandise/addorder/Addorder.vue'
import Orderdetails from '../views/oder/merchandise/orderdetails/Orderdetails.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: Layout,
    children: [
      {
        path: '',
        name: 'Home',
        component: Home,
        meta: {requiresAuth: true, breadcrumb: '首页' },
      },
      {
        path: 'product',
        name: 'Product',
        component: Product,
        meta: {requiresAuth: true, breadcrumb: '商品管理' },
      },
      {
        path: 'pettype',
        name: 'Pettype',
        component: Pettype,
        meta: {requiresAuth: true, breadcrumb: '宠物类型管理' },
      },
      {
        path: 'homecarousel',
        name: 'Homecarousel',
        component: Homecarousel,
        meta: {requiresAuth: true, breadcrumb: '轮播图管理' },
      },
      {
        path: 'petknowledge',
        name: 'Petknowledge',
        component: Petknowledge,
        meta: {requiresAuth: true, breadcrumb: '宣传栏管理' },
      },
      {
        path: 'Admin',
        name: 'Admin',
        component: Admin,
        meta: {requiresAuth: true, breadcrumb: '管理员管理' },
      },
      {
        path: 'user',
        name: 'User',
        component: User,
        meta: { breadcrumb: '用户管理' },
        children: [
          {
            path: 'membershiptype',
            name: 'Membershiptype',
            component: Membershiptype,
            meta: {requiresAuth: true, breadcrumb: '会员等级' },
          },
          {
            path: 'userlist',
            name: 'Userlist',
            component: Userlist,
            meta: {requiresAuth: true, breadcrumb: '用户管理' },
          },
        ],
      },
      {
        path: 'booking',
        name: 'Booking',
        component: Booking,
        meta: { breadcrumb: '预约管理' },
        children: [
          {
            path: 'service',
            name: 'Service',
            component: Service,
            meta: {requiresAuth: true, breadcrumb: '服务类型' },
          },
          {
            path: 'appointmentslot',
            name: 'Appointmentslot',
            component: Appointmentslot,
            meta: {requiresAuth: true, breadcrumb: '预约排期' },
          },
        ],
      },
      {
        path: 'oder',
        name: 'Oder',
        component: Oder,
        meta: { breadcrumb: '订单管理' },
        children: [
          {
            path: 'merchandise',
            name: 'Merchandise',
            component: Merchandise,
            meta: {requiresAuth: true, breadcrumb: '商品订单' },
          },
		  {
		    path: '/order/Addorder',
		    name: 'Addorder',
		    component: Addorder,
		    meta: { requiresAuth: true, breadcrumb: '新增订单' },
		  },
		  {
		    path: 'oder/Orderdetails/:id', // 将动态段作为路径的一部分
		    name: 'OrderDetails',
		    component: Orderdetails,
		    meta: { requiresAuth: true, breadcrumb: '订单详情' },
		  },
          {
            path: 'refund',
            name: 'Refund',
            component: Refund,
            meta: {requiresAuth: true, breadcrumb: '退款申请' },
          },
          {
            path: 'serviceappointment',
            name: 'Serviceappointment',
            component: Serviceappointment,
            meta: {requiresAuth: true, breadcrumb: '预约订单' },
          },
        ],
      },
    ],
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { breadcrumb: '登录' },
  },
  {
    path: '/update',
    name: 'Update',
    component: Update,
    meta: { breadcrumb: '更新' },
  },
]

const router = new VueRouter({
  routes,
  mode: 'history', // 使用 HTML5 history 模式
})

router.beforeEach((to, from, next) => {
  // 检查用户是否已经登录
  const isAuthRequired = to.matched.some(record => record.meta.requiresAuth);
  const isAuthenticated = localStorage.getItem('Authorization');

  if (isAuthRequired && !isAuthenticated) {
    
    next('/login');
  } else if (isAuthenticated && to.path === '/login') {
    // 用户已登录且试图访问登录页面，则重定向到主页
    next('/login');
  } else {
    // 其他情况，继续路由跳转
    next();
  }
})

export default router